﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Principal;


public partial class roles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Extract the forms authentication cookie
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        if (null == authCookie)
        {
            // There is no authentication cookie.
            return;
        }

        FormsAuthenticationTicket authTicket = null;
        try
        {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        }
        catch (Exception ex)
        {
            // Log exception details (omitted for simplicity)
            return;
        }

        if (null == authTicket)
        {
            // Cookie failed to decrypt.
            return;
        }

        string[] roles = authTicket.UserData.Split(new char[] { ',' });

        // Create an Identity object
        FormsIdentity id = new FormsIdentity(authTicket);

        // This principal will flow throughout the request.
        GenericPrincipal principal = new GenericPrincipal(id, roles);
        // Attach the new principal object to the current HttpContext object
        Context.User = principal;

        IPrincipal p = HttpContext.Current.User;
        Response.Write("Authenticated Identity is: " +
                        p.Identity.Name);
        Response.Write("<p>");

        if ( p.IsInRole("Author") )
          Response.Write( "User is in Senior Manager role<p>" );
        else
          Response.Write( "User is not in Senior Manager role<p>" );

        if (p.IsInRole("author"))
          Response.Write( "User is in Manager role<p>" );
        else
          Response.Write( "User is not in Manager role<p>" );

        if (p.IsInRole("author "))
          Response.Write( "User is in Employee role<p>" );
        else
          Response.Write( "User is not in Employee role<p>" );

        if (p.IsInRole(" author"))
          Response.Write( "User is in Sales role<p>" );
        else
          Response.Write( "User is not in Sales role<p>" );
       

    }
}
